<template>
	<view style="background-color:#FFF;">
		<fui-form ref="form">
			<fui-form-item label="企业名称" labelWidth="200" labelSize="26">
				<fui-input :borderBottom="false" :padding="[0]" placeholder="请输入企业名称" textRight
					v-model="formData.CompanyName"></fui-input>
			</fui-form-item>
			<fui-form-item label="营业执照代码" labelWidth="200" labelSize="26">
				<fui-input :borderBottom="false" :padding="[0]" placeholder="组织机构代码证/社会信用代码" textRight
					v-model="formData.IDCardCode"></fui-input>
			</fui-form-item>
			<view>
				<view style="padding:30rpx;">上传营业执照照片</view>
				<view class="flexbc" style="padding:40rpx;">
					<!-- 	<view style="width:45vw;">
						<view style="width:300rpx;height:400rpx;background-color:#f0f0f0;border-radius:20rpx;">
							
						</view>
						<view style="padding:10rpx;text-align:center;">营业执照示例</view>
					</view> -->
					<view style="width:100vw;">
						<!-- <view style="width:80vw;height:300rpx;background-color:#f0f0f0;border-radius:20rpx;"> -->
						<uni-file-picker limit="1" :del-icon="false" file-mediatype="image" v-model="IDCardPics"
							@fail="IDCardError" @select="uploadIDCard" :image-styles="imageStyles">+</uni-file-picker>
						<!-- </view> -->
						<view style="padding:10rpx;text-align:center;">点击上传营业执照</view>
						<view class="" style="text-align: center;">
							营业执照扫描打印，然后加盖“企业公章”，然后拍照上传（务必加盖公章）
						</view>
					</view>
				</view>
			</view>
			<fui-form-item label="联系电话" labelSize="26">
				<fui-input :borderBottom="false" :padding="[0]" placeholder="请输入认证人联系电话" textRight
					v-model="formData.Telphone"></fui-input>
			</fui-form-item>
			<view>
				<button class="submitBtn" @click="submitHandle">提交认证</button>
			</view>
			<view style="padding:30rpx;text-align:center;color:#FF6600">认证信息已加密，仅用于匹配认证</view>
			<!-- <view style="padding:20rpx;">
				温馨提示：<br>
				1、营业执照扫描打印，然后加盖“企业公章”，然后拍照上传（务必加盖公章）。
			</view> -->
		</fui-form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				formData: {
					CompanyName: '',
					IDCardCode: '',
					IDCardPics: '',
					Telphone: ''
				},
				imageStyles: {
					"width": '100%', // 边框宽度
					"height": 200, // 边框高度
					"border": false
					// { // 如果为 Boolean 值，可以控制边框显示与否
					// 	"color":"#e0e0e0",		// 边框颜色
					// 	"width":"1px",		// 边框宽度
					// 	"style":"solid", 	// 边框样式
					// 	"radius":"10rpx" 		// 边框圆角，支持百分比
					// }
				},
				IDCardPics: []
			};
		},
		onLoad() {
			this.getMyInfo()
		},
		methods: {
			getMyInfo() {
				this.$httpost('/UserCenter/myCompanyInfo', {}).then(res => {
					// console.log('myCompanyInfo', res)
					if (res.code == 200) {
						if (res.data !== null) {
							this.formData.CompanyName = res.data.CompanyName
							this.formData.IDCardCode = res.data.IDCardCode
							this.formData.IDCardPics = res.data.IDCardPics

							if (res.data.IDCardPics !== null) {
								let IDCard = {
									"name": res.data.IDCardPics.replace(
										'https://api.meimo.comdns.cn/public/upload/uniapp/CompanyAuth/', ''
										),
									"extname": ".jpg",
									"url": res.data.IDCardPics
								}

								this.IDCardPics.push(IDCard)
							}
							this.formData.Telphone = res.data.Telphone
							this.formData.CompanyMemo = res.data.CompanyMemo
						}
					}
				});
			},
			uploadIDCard(e) {
				// console.log('身份证正面上传', e)
				this.IDCardPics = e.tempFiles
			},
			IDCardError(e) {
				// console.log('身份证正面上传失败：',e)
			},
			submitHandle() {

				let _that = this;

				if (this.formData.CompanyName == '') {
					this.$common.showToast("请填写企业名称！");
					return false;
				}
				if (this.formData.IDCardCode == '') {
					this.$common.showToast("请填写营业执照代码！");
					return false;
				}
				if (this.IDCardPics.length == 0) {
					this.$common.showToast("请上传营业执照！");
					return false;
				}

				// console.log('this.IDCardPics', this.IDCardPics[0].url.split("://"))

				if (this.IDCardPics.length !== 0 && this.IDCardPics[0].url.split("://")[0] !== 'https') {
					uni.uploadFile({
						url: this.$imgUpload + '/CompanyAuth&type=image', //仅为示例，非真实的接口地址
						filePath: this.IDCardPics[0].path,
						name: "file",
						// files: tempFilePaths,
						formData: {},
						success: (uploadFileRes) => {
							// console.log('IDCardPics', uploadFileRes);

							let ResData = JSON.parse(uploadFileRes.data);

							this.formData.IDCardPics = this.$baseUrl + ResData.fileName;

							_that.saveAuth()
						}
					});
				} else {
					_that.saveAuth()
				}
			},
			saveAuth() {

				// console.log('this.formData', this.formData)

				this.$httpost('/UserAction/saveCompanyAuth', {
					formData: this.formData
				}).then(AuthRes => {
					// console.log('AuthRes', AuthRes)

					if (AuthRes.code == 200) {

						uni.showModal({
							title: '认证提交成功！请等待审核',
							showCancel: false,
							success: (res) => {
								uni.navigateBack()
							}
						})

					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.file-picker__box-content {
		height: 100%;
	}

	.submitBtn {
		width: 90vw;
		background-color: $globalColor;
		color: #000
	}

	.file-picker__progress {
		display: none;
	}
</style>